package com.hanxiaozhang.validation.util;



import com.hanxiaozhang.exception.ParameterNotValidException;

import java.math.BigDecimal;
import java.math.BigInteger;

/**
 * 〈一句话功能简述〉<br>
 * 〈校验工具类〉
 *  参考：com.google.common.base.Preconditions
 *
 * @author hanxinghua
 * @create 2020/7/24
 * @since 1.0.0
 */
public class ValidationUtil {

    private ValidationUtil() {}

    /**
     * 验证参数是否有效
     *
     * @param expression
     * @throws ParameterNotValidException 如果抛出异常 {@code expression} 是 false
     */
    public static void checkParameter(boolean expression) {
        if (!expression) {
            throw new ParameterNotValidException();
        }
    }

    /**
     * 验证参数是否有效
     *
     * @param expression
     * @param errorMessage 参数无效时使用的异常消息（可以为空）
     * @throws ParameterNotValidException 如果抛出异常 {@code expression} 是 false
     */
    public static void checkParameter(boolean expression, Object errorMessage) throws IllegalArgumentException {
        if (!expression) {
            throw new ParameterNotValidException(String.valueOf(errorMessage));
        }
    }


    /**
     * 判断对象是否不为空并且不为负数
     *
     * @param object
     * @return
     */
    public static boolean checkObjectIsNotNullAndNeg(Object object) {
        if (object == null) {
            return false;
        }
        if (object instanceof String) {
            return !"".equals(object);
        } else if (object instanceof BigDecimal) {
            return BigDecimal.ZERO.compareTo((BigDecimal) object) < 0;
        } else if (object instanceof Integer) {
            return (Integer) object >= 0;
        }  else if (object instanceof BigInteger) {
            return ((BigInteger) object).compareTo(BigInteger.valueOf(0)) >= 0;
        } else if (object instanceof Long) {
            return (Long) object > 0;
        } else {
            return true;
        }
    }

    /**
     * 判断对象是否不为空
     *
     * @param object
     * @return
     */
    public static boolean checkObjectIsNotNull(Object object) {
        if (object == null) {
            return false;
        }
        if (object instanceof String) {
            return !"".equals(object);
        } else {
            return true;
        }
    }


}
